// 引入服务
const connection = require('../../data')
// 我的班级-博文页
module.exports.myClassBlog = (req, res) => {
    if (!req.session.user) {
        res.render('login')
    } else {
        res.render('myClassBlog')
    }
}
// 我的班级-评论页
module.exports.myClassComment = (req, res) => {
    if (!req.session.user) {
        res.render('login')
    } else {
        res.render('myClassComment')
    }
}
// 我的班级-发布作业页
module.exports.publishJob = (req, res) => {
    res.render('publishJob')
}
// 我的班级-发布投票页
module.exports.startVote = (req, res) => {
    res.render('startVote')
}
// 我的班级-历史公告页
module.exports.historyNotice = (req, res) => {
    res.render('historyNotice')
}
// 显示最新公告
module.exports.showMyclassNewAnnounce = (req, res) => {
    connection.query('select * from classnotice where classnotice_time = (select max(classnotice_time) from classnotice)', (err, result) => {
        if (err) return console.log(err)
        res.json(result)
    })
}
// 显示所有公告
module.exports.showHistoryNotice = (req, res) => {
    connection.query('select * from classnotice', (err, result) => {
        if (err) return console.log(err)
        res.json(result)
    })
}
// 发布作业
module.exports.publishHomework = (req, res) => {
    let data = [null, req.body.jobTitle, req.body.classId, req.body.beginTime, req.body.endTime, null, req.body.jobContent, req.body.teacherId]
    let sql = 'insert into assignment values(?,?,?,?,?,?,?,?)'
    connection.query(sql, data, (err, result) => {
        if (err) return console.log(err)
        res.json({
            msg: '发布成功'
        })
    })
}
// 发布公告
module.exports.addAnnounce = (req, res) => {
    let data = [null, req.body.issuer, req.body.announce, null]
    let sql = 'insert into classnotice values(?,?,?,?)'
    connection.query(sql, data, (err, result) => {
        if (err) return console.log(err)
        res.json({
            msg: '发布公告成功'
        })
    })
}
// 删除公告
module.exports.delAnnounce = (req, res) => {
    connection.query('delete from classnotice where classnotice_id = ?', req.query.id, (err, result) => {
        if (err) return console.log(err)
        res.json({
            msg: '删除成功'
        })
    })
}
// 回显公告
module.exports.getAnnounce = (req, res) => {
    connection.query('select * from classnotice where classnotice_id = ?', req.query.id, (err, result) => {
        if (err) return console.log(err)
        res.json(result)
    })
}

// 更改公告
module.exports.changeAnnounce = (req, res) => {
    let data = [req.body.changeAnnounce, null, req.body.id]
    let sql = 'update classnotice set classnotice_content = ?,classnotice_time=? where classnotice_id = ?'
    connection.query(sql, data, (err, result) => {
        if (err) return console.log(err)
        res.json({
            msg: '修改成功'
        })
    })
}
// 显示单条博文
module.exports.showNewBlog = (req, res) => {
    connection.query('select * from blog where blog_time = (select max(blog_time) from blog)', (err, result) => {
        if (err) return console.log(err)
        res.json(result)
    })
}